<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ page
import="com.example.demo.grbkxt.model.User" %>
<!DOCTYPE html>
<html>
  <head>
    <title>编辑用户</title>
    <%@ include file="/WEB-INF/views/common/header.jsp" %>
    <style>
      .edit-form {
        max-width: 600px;
        margin: 20px auto;
        padding: 20px;
        background: #fff;
        border-radius: 8px;
        box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
      }
      .form-group {
        margin-bottom: 15px;
      }
      .form-group label {
        display: block;
        margin-bottom: 5px;
      }
      .form-group input {
        width: 100%;
        padding: 8px;
        border: 1px solid #ddd;
        border-radius: 4px;
      }
    </style>
  </head>
  <body>
    <div class="container">
      <div class="header">
        <h1>编辑用户</h1>
        <div class="nav-links">
          <a href="${pageContext.request.contextPath}/admin/users" class="btn"
            >返回用户列表</a
          >
        </div>
      </div>

      <div class="edit-form">
        <% User user = (User)request.getAttribute("user"); %>
        <form id="editForm" method="post" class="edit-form">
          <input type="hidden" name="id" value="<%= user.getId() %>" />

          <div class="form-group">
            <label>用户名：</label>
            <input
              type="text"
              name="username"
              value="<%= user.getUsername() %>"
              required
            />
          </div>

          <div class="form-group">
            <label>新密码：（留空表示不修改）</label>
            <input type="password" name="password" />
          </div>

          <div class="form-group">
            <label>
              <input type="checkbox" name="isAdmin" <%= user.isAdmin() ?
              "checked" : "" %>> 管理员权限
            </label>
          </div>

          <button type="submit" class="btn btn-primary">保存修改</button>
          <a
            href="${pageContext.request.contextPath}/admin/users"
            class="btn btn-secondary"
            >返回</a
          >
        </form>
      </div>

      <script>
        document
          .getElementById("editForm")
          .addEventListener("submit", function (e) {
            e.preventDefault();

            const formData = new FormData(this);
            const params = new URLSearchParams();
            for (const pair of formData) {
              params.append(pair[0], pair[1]);
            }

            fetch("${pageContext.request.contextPath}/admin/users/update", {
              method: "POST",
              headers: {
                "Content-Type": "application/x-www-form-urlencoded",
              },
              body: params,
            }).then((response) => {
              if (response.ok) {
                window.location.href =
                  "${pageContext.request.contextPath}/admin/users";
              } else {
                alert("更新失败");
              }
            });
          });
      </script>
    </div>
  </body>
</html>
